/*
 * @Description: App组件 - 系统目录自带组件
 * @Author: 谢涛
 * @LastEditors: 郑祥
 * @Date: 2019-04-22 19:13:58
 * @LastEditTime: 2022-07-28 16:54:34
 */

import React, { Component } from 'react';
import { message } from 'antd'
import './App.css';
import { Switch, Route, withRouter, Redirect } from 'react-router-dom'
import Login from './pages/Login'
import Index from './pages/Index'
import HomePage from './pages/HomePage'

message.config({
  top: 100,
  duration: 2,
  maxCount: 1,
});
function PrivateRoute({ component: RouteComponent, ...rest }) {
  return (
    <Route
      {...rest}
      render={props =>
        sessionStorage.getItem("isAuthenticated") || localStorage.getItem("isAuthenticated") ? (
          <RouteComponent {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/login",
              state: { from: props.location }
            }}
          />
        )
      }
    />
  );
}

class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      isAuthenticated: false
    }
  }
  static getDerivedStateFromProps() {
    let { search } = window.location
    let xtzh = search.split('xtzh=')[1]
    if (xtzh) {
      sessionStorage.setItem('isAuthenticated', true)
      return { isAuthenticated: true };
    }
    return null
  }
  render() {
    const { isAuthenticated } = this.state
    return (
        // <div>{
        //     isAuthenticated ? <Index></Index> : <Login></Login>
        // }</div>
      <Switch>
        <Route path='/login' exact component={Login} />
        {
          isAuthenticated ?
            <Route path='/homePage' component={HomePage} /> :
            <PrivateRoute path='/' component={Index} />
        }
      </Switch>
    );
  }
}

export default withRouter(App);